import json
def parser_symbol(s):
    tokens = []
    token = ''
    for c in s:
        if c == ' ' or c == '(' or c == ')' or c == '\n':
            if token:
                tokens.append(token)
                token = ''
            if c == ' ' or c == '\n':
                continue
            tokens.append(c)
            continue
        token += c
    return tokens


def parser_data(tokens):
    data_stack = []
    key_stack = []
    data = []
    for i, v in enumerate(tokens):
        if v == '(':
            data_stack.append(data)
            data = []
        elif v == ")":
            x = data_stack.pop()
            if isinstance(x , dict):
                key = key_stack.pop()
                x[key] = data
            else:
                x.append(data)
            data = x
        else:
            if tokens[i+1] == "(" and data == []:
                data = {}
                key_stack.append(v)
            elif isinstance(data, list):
                data.append(v)
            elif isinstance(data, dict):
                key_stack.append(v)
            else:
                print("error")

    return data





if __name__ == '__main__':
    fs = open("display.drf")
    lines = fs.readlines()
    lines = [x.strip() for x in lines]
    lines = [x for x in lines if not x.startswith(";")]
    s = '\n'.join(lines)
    tokens = parser_symbol(s)
    data = parser_data(tokens)
    with open("display_out.json" , "w") as fs:
        json.dump(data, fs ,indent=4)
